#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
TaoBao爬虫配置文件
"""

import os
from faker import Faker

class TaoBaoConfig:
    """TaoBao爬虫配置类"""
    
    # 基础URL配置
    BASE_URL = "https://www.taobao.com"
    LOGIN_URL = "https://login.taobao.com/member/login.jhtml"
    
    # 超时配置
    DEFAULT_TIMEOUT = 30
    LOGIN_TIMEOUT = 60
    PAGE_LOAD_TIMEOUT = 30
    
    # 浏览器配置
    CHROME_DRIVER_PATH = os.path.join(os.path.dirname(__file__), "chromedriver.exe")
    CHROME_OPTIONS = [
        "--no-sandbox",
        "--disable-dev-shm-usage",
        "--disable-blink-features=AutomationControlled",
        "--exclude-switches=enable-automation",
        "--disable-extensions-file-access-check",
        "--disable-extensions",
        "--disable-plugins-discovery",
        "--start-maximized"
    ]
    
    # 等待时间配置
    WAIT_TIMES = {
        'short': 2,
        'medium': 5,
        'long': 10,
        'login': 60,
        'page_load': 15
    }
    
    # 选择器配置 (基于shoplist分析)
    SELECTORS = {
        'login': {
            'login_button_nav': 'a.h',  # 顶部导航登录按钮 "亲，请登录"
            'login_button_page': 'div.loginButton--LJZtcb3I',  # 页面登录按钮 "立即登录"
            'login_text_indicator': 'div.loginText--kGn7L3c4',  # 登录提示文本
            'login_container': 'div.loginContainer--SIzDZs8Y',  # 登录容器
            # 手机版和其他登录入口
            'mobile_login_buttons': [
                'div[data-spm*="login"]',  # 包含login的data-spm属性
                'a[href*="login"]',  # 包含login的链接
                'button[class*="login"]',  # 包含login的按钮
                'div[class*="login"]',  # 包含login的div
                '.login-btn, .login-button',  # 常见登录按钮类名
                '[data-action*="login"]',  # data-action包含login
                'span:contains("登录")',  # 包含"登录"文本的span
                'div:contains("账号密码登录")',  # 包含"账号密码登录"的div
                'a:contains("账号密码登录")',  # 包含"账号密码登录"的链接
                'button:contains("账号密码登录")'  # 包含"账号密码登录"的按钮
            ],
            # 登录页面的实际元素
            'login_form': 'form#login-form, .login-form',  # 登录表单
            'username_input': 'input#fm-login-id, input[name="fm-login-id"], input.fm-text[placeholder*="账号"]',
            'password_input': 'input#fm-login-password, input[name="fm-login-password"], input[type="password"].fm-text',
            'submit_button': 'button.fm-submit, button.password-login, .fm-button',
            'captcha_container': '#baxia-password, .baxia-container, .tb-login',
            'captcha_input': 'input#fm-login-checkcode, .fm-checkcode',
            'agreement_checkbox': 'input#fm-agreement-checkbox, input[name="fm-agreement-checkbox"], input[type="checkbox"][id*="agreement"]',
            'login_success_indicator': '.site-nav-user, .user-info',
            # 登录页面检测
            'login_page_indicator': '#login-form, .login-box, #container, .login-content',
            # 协议弹框
            'agreement_dialog': '.login-dialog, div[role="document"].login-dialog',
            'agreement_dialog_title': '.login-dialog-title, [id*="DialogTitle"]',
            'agreement_agree_button': '.dialog-btn-ok, button.dialog-btn-ok',
            'agreement_cancel_button': '.dialog-btn-cancel, button.dialog-btn-cancel'
        },
        'shop': {
            'shop_name': 'span.shopName--qijIco3R, .shopName--DFnW8rfp',
            'shop_logo': 'img.shopAvatar--VGD3wST1, .shopLogoUrlWrapper--mCEJHRzE img',
            'product_grid': 'div.container--JVhREvQT',
            'product_container': 'div[data-spm="product_shelf"]'
        },
        'product_list': {
            'product_cards': 'div.cardContainer--CwazTl0O',
            'product_title': 'div.title--GExDBPUi',
            'product_price': 'div.price--WtT08bds',
            'product_count': 'div.count--nrEC6YGF',
            'product_image': 'img',
            'product_link': 'a',
            'product_tags': '.tags--ejYQ37YS span',
            'next_page': '.next-page, .pagination-next',
            'load_more': 'div.loadStatus--ctfp48MP'
        },
        'product_detail': {
            'title': '',
            'price': '',
            'original_price': '',
            'description': '',
            'specifications': '',
            'images': '',
            'shop_info': '',
            'sales_info': ''
        }
    }
    
    # 日志配置
    LOG_LEVEL = "INFO"
    LOG_FILE = "taobao_scraper.log"
    LOG_FORMAT = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
    
    # 输出配置
    OUTPUT_DIR = "."
    PRODUCT_LIST_FILE = "taobao_products.json"
    PRODUCT_DETAIL_FILE = "taobao_products_detailed.json"
    API_FORMAT_FILE = "taobao_products_api_format.json"
    
    # API配置 (参考其他平台)
    API_ENDPOINT = ""
    API_HEADERS = {
        'Content-Type': 'application/json',
        'User-Agent': ''
    }
    
    # Redis配置 (预留)
    REDIS_CONFIG = {
        'host': '',
        'port': 6379,
        'password': '',
        'task_key': ''
    }
    
    @classmethod
    def get_headers(cls):
        """获取请求头"""
        fake = Faker()
        return {
            'User-Agent': fake.user_agent(),
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
            'Accept-Encoding': 'gzip, deflate, br',
            'DNT': '1',
            'Connection': 'keep-alive',
            'Upgrade-Insecure-Requests': '1',
        }
    
    @classmethod
    def get_random_user_agent(cls):
        """获取随机User-Agent"""
        fake = Faker()
        return fake.user_agent() 